<!--
 * @Description: 订单管理
 * @Author: xiao li
 * @Date: 2021-07-03 11:41:05
 * @LastEditTime: 2022-09-09 17:05:57
 * @LastEditors: xiao li
-->

<template>
  <div class="lb-shop-order">
    <top-nav />
    <div class="page-main">
      <el-row class="page-search-form">
        <el-form
          @submit.native.prevent
          :inline="true"
          :model="searchForm"
          ref="searchForm"
        >
          <el-form-item label="服务名称" prop="goods_name">
            <el-input
              v-model="searchForm.goods_name"
              placeholder="请输入服务名称"
            ></el-input>
          </el-form-item>
          <el-form-item label="技师姓名" prop="coach_name">
            <el-input
              v-model="searchForm.coach_name"
              placeholder="请输入技师姓名"
            ></el-input>
          </el-form-item>
          <el-form-item label="渠道商姓名" prop="channel_name">
            <el-input
              v-model="searchForm.channel_name"
              placeholder="请输入渠道商姓名"
            ></el-input>
          </el-form-item>
          <el-form-item label="渠道" prop="channel_cate_id">
            <el-select
              @change="getTableDataList(1)"
              v-model="searchForm.channel_cate_id"
              filterable
              clearable
              placeholder="请选择"
            >
              <el-option
                v-for="item in base_channel"
                :key="item.id"
                :label="item.title"
                :value="item.id"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="系统订单号" prop="order_code">
            <el-input
              v-model="searchForm.order_code"
              placeholder="请输入系统订单号"
            ></el-input>
          </el-form-item>
          <el-form-item label="下单手机号" prop="mobile">
            <el-input
              v-model="searchForm.mobile"
              placeholder="请输入下单时填写的手机号"
              style="width: 200px"
            ></el-input>
          </el-form-item>
          <el-form-item label="日期" prop="start_time">
            <el-date-picker
              @change="getTableDataList(1)"
              v-model="range"
              type="daterange"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              value-format="timestamp"
              :picker-options="pickerOptions"
            >
            </el-date-picker>
          </el-form-item>
          <el-form-item label="状态" prop="pay_type">
            <el-select
              @change="getTableDataList(1)"
              v-model="searchForm.pay_type"
              placeholder="请选择"
            >
              <el-option
                v-for="item in statusOptions"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <lb-button
              size="medium"
              type="primary"
              icon="el-icon-search"
              style="margin-right: 5px"
              @click="getTableDataList(1)"
              >{{ $t('action.search') }}</lb-button
            >
            <lb-button
              size="medium"
              icon="el-icon-refresh-left"
              style="margin-right: 5px"
              @click="resetForm('searchForm')"
              >{{ $t('action.reset') }}</lb-button
            >
          </el-form-item>
        </el-form>
      </el-row>
      <el-row class="page-top-operate">
        <lb-button
          size="mini"
          plain
          type="primary"
          icon="el-icon-printer"
          @click="printTable"
          >打印</lb-button
        >
        <lb-button
          size="mini"
          plain
          type="primary"
          icon="el-icon-download"
          :loading="downloadLoading"
          @click="exportOrder"
          >导出</lb-button
        >
        <div>
          共{{ total }}条数据，订单金额共计：{{ order_price }}元<lb-tool-tips
            :padding="0"
            >此处订单金额不包含出行费用和已退款金额</lb-tool-tips
          >
        </div>
      </el-row>
      <el-table
        v-loading="loading"
        :data="tableData"
        :header-cell-style="{ background: '#f5f7fa', color: '#606266' }"
        style="width: 100%"
      >
        <el-table-column
          prop="id"
          label="ID"
          width="80"
          fixed
        ></el-table-column>
        <el-table-column
          prop="goods_info_text"
          width="300"
          label="服务项目信息"
        >
          <template slot-scope="scope">
            <div
              class="pb-sm"
              v-for="(citem, cindex) in scope.row.order_goods"
              :key="cindex"
            >
              <div class="flex-center pt-md">
                <lb-image class="avatar radius-5" :src="citem.goods_cover" />
                <div
                  class="flex-1 f-caption c-caption ml-md"
                  style="width: 210px"
                >
                  <div class="flex-between">
                    <div
                      class="f-paragraph c-title ellipsis"
                      :class="[{ 'max-300': citem.refund_num > 0 }]"
                      style="line-height: 1.2; margin-bottom: 4px"
                    >
                      {{ citem.goods_name }}
                    </div>
                    <div
                      class="f-caption c-warning"
                      v-if="citem.refund_num > 0"
                    >
                      已退x{{ citem.refund_num }}
                    </div>
                  </div>
                  <div class="flex-between f-caption mt-md">
                    <div class="c-warning">¥{{ citem.price }}</div>
                    <div>x{{ citem.num }}</div>
                  </div>
                </div>
              </div>
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="user_name" label="下单人"></el-table-column>
        <el-table-column
          prop="mobile"
          width="120"
          label="下单手机号"
        ></el-table-column>
        <el-table-column prop="coach_name" label="技师"></el-table-column>
        <el-table-column prop="channel_name" label="渠道商"></el-table-column>
        <el-table-column prop="channel" label="渠道"></el-table-column>
        <el-table-column prop="start_time" min-width="120" label="服务开始时间">
          <template slot-scope="scope">
            <div>{{ scope.row.start_time | handleTime(1) }}</div>
            <div>{{ scope.row.start_time | handleTime(2) }}</div>
          </template></el-table-column
        >
        <el-table-column prop="car_type" min-width="250" label="出行费用">
          <template slot-scope="scope">
            <div>
              {{ carType[scope.row.car_type] }}
            </div>

            <div class="flex-y-center" v-if="scope.row.car_type == 1">
              全程{{ scope.row.distance }}，车费¥{{ scope.row.car_price }}
            </div>
          </template>
        </el-table-column>
        <el-table-column
          prop="init_service_price"
          width="120"
          label="服务项目费用"
        >
          <template slot-scope="scope">
            ¥{{ scope.row.init_service_price }}
          </template>
        </el-table-column>
        <el-table-column prop="pay_price" label="实收金额">
          <template slot-scope="scope">
            <div>¥{{ scope.row.pay_price }}</div>
          </template>
        </el-table-column>
        <el-table-column prop="refund_price" label="退款金额">
          <template slot-scope="scope" v-if="scope.row.refund_price">
            <div>¥{{ scope.row.refund_price }}</div>
          </template>
        </el-table-column>
        <el-table-column
          prop="order_code"
          width="150"
          label="系统订单号"
        ></el-table-column>
        <el-table-column
          prop="transaction_id"
          width="150"
          label="付款订单号"
        ></el-table-column>
        <el-table-column prop="create_time" min-width="120" label="下单时间">
          <template slot-scope="scope">
            <div>{{ scope.row.create_time | handleTime(1) }}</div>
            <div>{{ scope.row.create_time | handleTime(2) }}</div>
          </template>
        </el-table-column>
        <el-table-column prop="is_balance" label="支付方式">
          <template slot-scope="scope">
            {{ payType[scope.row.is_balance] }}
          </template>
        </el-table-column>
        <el-table-column prop="status" label="状态">
          <template slot-scope="scope">
            {{ statusType[scope.row.pay_type] }}
          </template>
        </el-table-column>
      </el-table>
      <!-- 打印表格 -->
      <table
        v-show="print"
        style="font-size: 12px"
        ref="print"
        border="0"
        cellspacing="0"
        cellapdding="0"
      >
        <thead>
          <tr>
            <th
              style="border: 1px solid"
              v-for="(item, index) in printTableData[0]"
              :key="index"
            >
              {{ item }}
            </th>
          </tr>
        </thead>
        <tbody>
          <tr v-for="(item, index) in printTableData[1]" :key="index">
            <td style="border: 1px solid" v-for="(items, i) in item" :key="i">
              {{ items }}
            </td>
          </tr>
        </tbody>
      </table>
      <lb-page
        :batch="false"
        :page="searchForm.page"
        :pageSize="searchForm.limit"
        :total="total"
        @handleSizeChange="handleSizeChange"
        @handleCurrentChange="handleCurrentChange"
      >
      </lb-page>
    </div>
  </div>
</template>

<script>
import moment from 'moment'
import pubic from '@/utils/public'
export default {
  data () {
    return {
      loading: false,
      downloadLoading: false,
      print: false,
      searchForm: {
        page: 1,
        limit: 10,
        goods_name: '',
        coach_name: '',
        is_channel: 1,
        channel_name: '',
        channel_cate_id: 0,
        order_code: '',
        mobile: '',
        pay_type: 0
      },
      base_channel: [],
      pickerOptions: {
        disabledDate (time) {
          return time.getTime() > Date.now()
        }
      },
      range: '',
      statusOptions: [
        {
          label: '全部订单',
          value: 0
        },
        // {
        //   label: '已取消',
        //   value: -1
        // },
        // {
        //   label: '待支付',
        //   value: 1
        // },
        {
          label: '待服务',
          value: 2
        },
        {
          label: '技师接单',
          value: 3
        },
        {
          label: '技师出发',
          value: 4
        },
        {
          label: '技师到达',
          value: 5
        },
        {
          label: '服务中',
          value: 6
        },
        {
          label: '已完成',
          value: 7
        }
      ],
      carType: {
        0: '公交/地铁',
        1: '出租车'
      },
      payType: {
        0: '微信支付',
        1: '账户余额'
      },
      statusType: {
        '-1': '已取消',
        1: '待支付',
        2: '待服务',
        3: '技师接单',
        4: '技师出发',
        5: '技师到达',
        6: '服务中',
        7: '已完成'
      },
      printTableData: [],
      tableData: [],
      total: 0,
      order_price: 0
    }
  },
  async created () {
    await this.getBaseInfo()
    this.getTableDataList()
  },
  methods: {
    async getBaseInfo () {
      let { data } = await this.$api.channel.cateSelect()
      data.unshift({
        id: 0,
        title: '全部'
      })
      this.base_channel = data
    },
    resetForm (form) {
      this.range = ''
      this.$refs[form].resetFields()
      this.getTableDataList(1)
    },
    handleSizeChange (val) {
      this.searchForm.limit = val
      this.handleCurrentChange(1)
    },
    handleCurrentChange (val) {
      this.searchForm.page = val
      this.getTableDataList()
    },
    /**
     * @method 搜索订单列表
     */
    getTableDataList (flag) {
      if (flag) this.searchForm.page = 1
      this.loading = true
      let searchForm = JSON.parse(JSON.stringify(this.searchForm))
      let range = JSON.parse(JSON.stringify(this.range))
      if (range && range.length > 0) {
        searchForm.start_time = parseInt(range[0] / 1000)
        searchForm.end_time = parseInt(range[1] / 1000) + 24 * 3600 - 1
      } else {
        searchForm.start_time = ''
        searchForm.end_time = ''
      }
      this.$api.shop.orderList(searchForm).then(res => {
        this.loading = false
        if (res.code === 200) {
          let { data, total, order_price: price } = res.data
          data.map(item => {
            item.is_balance = item.balance * 1 > 0 ? 1 : 0
            item.refund_price = item.refund_price * 1 > 0 ? item.refund_price : ''
          })
          this.tableData = data
          this.total = total
          this.order_price = price
          let arr = data.map(item => {
            item.goods_text = ``
            item.order_goods.map(aitem => {
              item.goods_text += `${aitem.goods_name} 数量：x${aitem.num}；`
            })
            let car = item.car_type === 1 ? `全程${item.distance}，车费${item.car_price}` : ``
            item.car_text = `${this.carType[item.car_type]}${car}`
            return [
              item.id,
              item.goods_text,
              item.user_name,
              item.mobile,
              item.coach_name,
              item.channel_name,
              item.channel,
              moment(item.start_time * 1000).format('YYYY-MM-DD HH:mm:ss'),
              item.car_text,
              item.init_service_price,
              item.pay_price,
              item.refund_price,
              item.order_code,
              item.transaction_id,
              moment(item.create_time * 1000).format('YYYY-MM-DD HH:mm:ss'),
              this.payType[item.is_balance],
              this.statusType[item.pay_type]
            ]
          })
          this.printTableData = [
            [
              'ID',
              '服务项目信息',
              '下单人',
              '下单手机号',
              '技师',
              '渠道商',
              '渠道',
              '服务开始时间',
              '出行费用',
              '服务项目费用',
              '实收金额',
              '退款金额',
              '系统订单号',
              '付款订单号',
              '下单时间',
              '支付方式',
              '状态'
            ],
            arr
          ]
        }
      })
    },

    /**
     * @method 导出订单
     */
    exportOrder () {
      this.downloadLoading = true
      let searchForm = JSON.parse(JSON.stringify(this.searchForm))
      let range = JSON.parse(JSON.stringify(this.range))
      if (range && range.length > 0) {
        searchForm.start_time = range[0] / 1000
        searchForm.end_time = range[1] / 1000 + 24 * 3600 - 1
      } else {
        searchForm.start_time = ''
        searchForm.end_time = ''
      }
      let url = pubic.getProCurrentHref()
      let keywords = url.indexOf('?') > 0 ? '' : '?'
      let flag = url.indexOf('?') > 0
      Object.getOwnPropertyNames(searchForm).forEach((keys, value) => {
        keywords += flag
          ? `&${keys}=${searchForm[keys]}`
          : `${keys}=${searchForm[keys]}`
        flag = true
      })
      let token = sessionStorage.getItem('minitk')
      let dwonlaodUrl = `${url}/massage/admin/AdminExcel/orderList${keywords}&token=${token}`
      window.location.href = dwonlaodUrl
      setTimeout(() => {
        this.downloadLoading = false
      }, 5000)
    },
    /**
     * @method 跳转详情
     */
    jumpToDetail () {
      this.$router.push()
    },
    /**
     * @method 打印表格
     */
    printTable () {
      this.print = true
      setTimeout(() => {
        this.$print(this.$refs.print)
        this.print = false
      }, 50)
    }
  },
  filters: {
    handleTime (val, type) {
      let time = type === 1 ? moment(val * 1000).format('YYYY-MM-DD') : type === 2 ? moment(val * 1000).format('HH:mm:ss') : moment(val * 1000).format('YYYY-MM-DD HH:mm:ss')
      return time
    }
  }
}
</script>

<style lang="scss" scoped>
.lb-shop-order {
  .page-main {
    width: 100%;

    .el-select,
    .el-input-number,
    .el-input {
      width: 200px;
    }
  }
}

.none {
  display: none;
}
</style>
<style media="print">
.none {
  display: block;
}
</style>
